package com.manage.sysuser.controller;

import java.util.HashMap;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.manage.base.bean.CommonResultBean;
import com.manage.base.controller.BaseController;
import com.manage.config.model.Config;
import com.manage.sysuser.model.SysUser;
import com.manage.sysuser.service.ISysUserService;

/**
 * 
 * @author EuroSun
 *
 */
@Controller
@RequestMapping("/sysuser")
public class SysUserController extends BaseController {

	private static final long serialVersionUID = 1L;

	static final Logger LOG = LoggerFactory.getLogger(SysUserController.class);

	@Autowired
	private ISysUserService sysUserService;

	/**
	 * 系统用户查询
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/select", method = RequestMethod.GET)
	@ResponseBody
	public CommonResultBean select(String data) {
		LOG.info("call sysuser select data = " + data);
		try {
			SysUser e = gson.fromJson(data, SysUser.class);
			List<SysUser> sysUsers = sysUserService.selectList(e);
			int size = sysUserService.selectTotalCount(e);
			CommonResultBean commonResultBean = new CommonResultBean();
			commonResultBean.setResult_code(Config.SUCCESS);
			commonResultBean.setResult_message(Config.SUCCESS_MSG);
			HashMap<String, Object> result_map = new HashMap<String, Object>();
			result_map.put("size", size);
			result_map.put("sysUsers", sysUsers);
			commonResultBean.setResult_map(result_map);
			return commonResultBean;
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
			CommonResultBean commonResultBean = new CommonResultBean();
			commonResultBean.setResult_code(Config.FAILE);
			commonResultBean.setResult_message(Config.ERROR_MSG);
			return commonResultBean;
		}
	}

	/**
	 * 系统用户操作
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/op", method = RequestMethod.POST)
	@ResponseBody
	public CommonResultBean op(String data) {
		LOG.info("call sysuser op data = " + data);
		try {
			SysUser sysUser = gson.fromJson(data, SysUser.class);
			sysUserService.op(sysUser);
			CommonResultBean commonResultBean = new CommonResultBean();
			commonResultBean.setResult_code(Config.SUCCESS);
			commonResultBean.setResult_message(Config.SUCCESS_MSG);
			return commonResultBean;
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
			CommonResultBean commonResultBean = new CommonResultBean();
			commonResultBean.setResult_code(Config.FAILE);
			commonResultBean.setResult_message(Config.ERROR_MSG);
			return commonResultBean;
		}
	}

	/**
	 * 系统用户添加
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public CommonResultBean add(String data) {
		LOG.info("call sysuser add data = " + data);
		try {
			SysUser sysUser = gson.fromJson(data, SysUser.class);
			// int i=
			sysUserService.add(sysUser);
			sysUserService.addRoleUser(sysUser);
			CommonResultBean commonResultBean = new CommonResultBean();
			commonResultBean.setResult_code(Config.SUCCESS);
			commonResultBean.setResult_message(Config.SUCCESS_MSG);
			return commonResultBean;
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
			CommonResultBean commonResultBean = new CommonResultBean();
			commonResultBean.setResult_code(Config.FAILE);
			commonResultBean.setResult_message(Config.ERROR_MSG);
			return commonResultBean;
		}
	}

	/**
	 * 系统用户修改
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public CommonResultBean update(String data) {
		LOG.info("call sysuser update data = " + data);
		try {
			SysUser sysUser = gson.fromJson(data, SysUser.class);
			if (sysUser.isPasswordModify()) {
				String old = sysUser.getOldPassword();
				String cur = sysUserService.selectOne(sysUser).getUserPassword();
				if (!cur.equals(old)) {
					CommonResultBean bean = CommonResultBean.errorBean();
					bean.setErrorMsg("原有密码填写失败");
					return bean;
				}
			}
			sysUserService.update(sysUser);
			return CommonResultBean.succBean();
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
			return CommonResultBean.errorBean();
		}
	}

	/**
	 * 系统用户详情
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/detail", method = RequestMethod.GET)
	@ResponseBody
	public CommonResultBean profile(String data) {
		LOG.info("call sysuser detail data = " + data);
		try {
			SysUser sysUser = gson.fromJson(data, SysUser.class);
			sysUser = sysUserService.selectOne(sysUser);
			sysUser.setUserPassword("");
			CommonResultBean commonResultBean = new CommonResultBean();
			HashMap<String, Object> result_map = new HashMap<String, Object>();
			result_map.put("sysUser", sysUser);
			commonResultBean.setResult_map(result_map);
			return commonResultBean;
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
			CommonResultBean commonResultBean = new CommonResultBean();
			commonResultBean.setError();
			return commonResultBean;
		}
	}

	/**
	 * 系统用户删除
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public CommonResultBean delete(String data) {
		LOG.info("call sysuser delete data = " + data);
		try {
			SysUser sysUser = gson.fromJson(data, SysUser.class);
			sysUserService.delete(sysUser);
			CommonResultBean commonResultBean = new CommonResultBean();
			commonResultBean.setResult_code(Config.SUCCESS);
			commonResultBean.setResult_message(Config.SUCCESS_MSG);
			return commonResultBean;
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
			CommonResultBean commonResultBean = new CommonResultBean();
			commonResultBean.setResult_code(Config.FAILE);
			commonResultBean.setResult_message(Config.ERROR_MSG);
			return commonResultBean;
		}
	}

}
